<template>
  <el-container class="home-container">
    <!-- 头部区-->
    <el-header>
      <div class="divimg">
        <img src="../assets/logo.png" />
        <span>管理后台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区 -->
    <el-container>
      <!-- 左侧区 -->
      <el-aside :width="isCollapes ? '64px' : '200px'">
        <!-- 左侧菜单区上的折叠展开区 -->
        <div class="toggle-button " @click="toggleCollapse()">|||</div>
        <!-- 左侧菜单区 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409BFF"
          :unique-opened="true"
          :collapse="isCollapes"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="IconImgs[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区 -->
      <el-main>
        <!--路由占位符 -->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  // 数据区
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      // 左侧图标
      IconImgs: {
        '125': 'iconfont icon-dingdan',
        '103': 'iconfont icon-paihangbang',
        '101': 'iconfont icon-lihe',
        '102': 'iconfont icon-mingxinghuodong',
        '145': 'iconfont icon-shangquan'
      },
      // 是否折叠
      isCollapes: false,
      // 当前激活链接
      activePath: null
    }
  },
  // created 区
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  // 行为区
  methods: {
    // 退出登录
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 左侧菜单的折叠展开
    toggleCollapse () {
      this.isCollapes = !this.isCollapes
    },
    // 保存连接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .divimg {
    display: flex;
    align-items: center;

    img {
      font-size: 20px;
      width: 50px;
      height: 50px;
    }
    span {
      color: azure;
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: aliceblue;
}
.iconfont {
  margin: 10px;
}
.toggle-button {
  background-color: #4a5064;
  color: #ffffff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
}
</style>
